/**
 * Classname :AccountModel.java
 *
 * Version information: 1.0
 *
 * Date: Dec 27, 2010 ho.viet.duan create
 *
 * Planex Viet Name co.ltd, All Rights Reserved.
 */

package com.viviet.mobileservices.domain.model;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.GrantedAuthorityImpl;
import org.springframework.security.userdetails.UserDetails;

import com.viviet.mobileservices.dao.entity.VMCustomer;

public class WebUserDetails implements UserDetails {
	private final String USER_ATTR = "USER";
	private static final long serialVersionUID = 5246495026860550557L;
	
	private final Map<String, Serializable> attrs = new HashMap<String, Serializable>();
	protected GrantedAuthority[] roles = null;

	public WebUserDetails(VMCustomer customer, String role) {
        if (customer != null) {
            // +++
        	addAttr(USER_ATTR, customer);
        	
            // +++ add roles
            if (StringUtils.isNotBlank(role)) {
            	this.roles = new GrantedAuthorityImpl[1];
                int index = 0;   
                this.roles[index] = new GrantedAuthorityImpl(role);
            }            
        }
    }
	
	/**
     * 
     * @return
     */
    public VMCustomer getUser() {
    	try {
    		return (VMCustomer)this.getAttr(USER_ATTR);
    	} catch (Exception e) {
    		return null;
    	}
    }
    /* (non-Javadoc)
	 * @see org.springframework.security.userdetails.UserDetails#getUsername()
	 */
    public String getUserId() {
    	return getUser().getLoginId();
	}
    /* (non-Javadoc)
	 * @see org.springframework.security.userdetails.UserDetails#getUsername()
	 */
	public String getUsername() {
		return getUser().getLoginId();
	}

	/**
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public final boolean addAttr(String key, Serializable value) {
		if(key != null && value != null) {
			attrs.put(key, value);
			return true;
		}
		return false;
	}
	
	/**
	 * 
	 * @param key
	 * @return
	 */
	public final Serializable getAttr(String key) {
		if(key != null) {
			return attrs.get(key);
		}
		return null;
	}
	
	/**
	 * 
	 * @return
	 */
	public final Set<String> getAttrKeys() {
		return attrs.keySet();
	}
	
    /*
     * (non-Javadoc)
     * 
     * @see org.springframework.security.userdetails.UserDetails#getAuthorities()
     */
    public GrantedAuthority[] getAuthorities() {
        return roles;
    }
    
    /* 
	 * Indicates whether the user is enabled or disabled. A disabled user cannot
     * be authenticated. true if the user is enabled, false otherwise
     * 
     * (non-Javadoc)
	 * @see org.springframework.security.userdetails.UserDetails#isEnabled()
	 */
	@Override
	public boolean isEnabled() {
		// always allow login
		return true;
	}

	@Override
	public String getPassword() {
		VMCustomer user = this.getUser();
		return (user == null) ? null : user.getLoginPass();
	}

	@Override
	public boolean isAccountNonExpired() {
		return true;
	}

	@Override
	public boolean isAccountNonLocked() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isCredentialsNonExpired() {
		// TODO Auto-generated method stub
		return true;
	}
}
